package voip.capture.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.app.Activity;
import android.app.Service;
import android.content.Intent;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.media.MediaRecorder.AudioSource;

public class VoiceCaptureServer extends Service
{
	Worker w;
	String IP;
	Intent me;

	@Override
	public int onStartCommand(Intent intent, int flags, int startId){
		super.onStartCommand(intent, flags,startId);

		
		Worker w = new Worker(intent.getExtras().getString("User_IP"));
		Thread t = new Thread(w);
		t.start();

		return START_STICKY;
	}

	@Override
	public void onDestroy() {
		w.stop();
	}

	public class Worker implements Runnable {
		String Ipz;


		public Worker(String i){
			this.Ipz = i;
		}

		private boolean stopped = false;

		static final int CLIENT_PORT = 4446; //Outgoing
		static final int SERVER_FEED = 4449; //Outgoing

		private int audioSource = MediaRecorder.AudioSource.MIC;
		private int samplingRate = 44100; /* in Hz*/
		private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
		private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
		private int BUF_SIZE = samplingRate ;



		private String LOG_TAG = "VOICE CAPTURE SERVICE";

		private boolean isRecording = false;




		@Override
		public void run() 
		{
			AudioRecord recorder = new AudioRecord(AudioSource.MIC, samplingRate, channelConfig, audioFormat, BUF_SIZE);
			recorder.startRecording();


			try{
				Log.d("SERVER","SERVER IS STARTING");


				isRecording = true;
				//capture data and record to file
				byte[] data = new byte[BUF_SIZE]; 
				int readBytes=0;



				InetAddress inet = InetAddress.getByName(this.Ipz);

			//	Log.d("VOICE CAPTURE SERVICE","Ip is " + this.Ipz);


				DatagramSocket sock = new DatagramSocket(SERVER_FEED);
				DatagramPacket p;

				Log.d("Server", "We are sending to " + this.Ipz );

				do{
					readBytes = recorder.read(data, 0, BUF_SIZE);	//Read audio data from the recorder
					p = new DatagramPacket(data,readBytes,inet,CLIENT_PORT);	//Write audio data to packet
					if(AudioRecord.ERROR_INVALID_OPERATION != readBytes){
					//	Log.d("VOICE CAPTURE SERVICE","Sent Packet");

						sock.send(p);	//Send packet 
					} 
					else{
						this.stop();	//Stop sending
					}
				}
				while(isRecording);

				sock.close();	//Close Socket
			} catch(Exception e){
				Log.d("BLAMMY", "ERROR");
				Log.d("BOB",e.getMessage());
			}
		}

		protected void stop()
		{ 
			isRecording = false;
		}

	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}


}



















